package com.aismy.admin.controller;

import com.aismy.admin.dto.UpdateUserDto;
import com.aismy.admin.dto.UserDTO;
import com.aismy.admin.entity.SysUser;
import com.aismy.admin.service.UserService;
import com.aismy.admin.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/user")
@RequiredArgsConstructor
public class AdminUserController {

    private final UserService userService;

    @GetMapping("/list")
    public Result<Page<SysUser>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String query) {
        return Result.success(userService.getUserPage(page, size, query));
    }

    @PostMapping
    public Result<Void> add(@RequestBody UserDTO userDTO) {
        userService.addUser(userDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        userDTO.setId(id);
        userService.updateUser(userDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        userService.updateStatus(id, userDTO.getStatus());
        return Result.success();
    }

    @GetMapping("/experts")
    public Result<Page<SysUser>> expertList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String query) {
        return Result.success(userService.getExpertPage(page, size, query));
    }

    @GetMapping("/consultants")
    public Result<Page<SysUser>> consultantList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String query) {
        return Result.success(userService.getConsultantPage(page, size, query));
    }

    @PutMapping("/{id}/consultant")
    public Result<Void> updateExpertStatus(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        userDTO.setId(id);
        userService.updateUser(userDTO);
        return Result.success();
    }

    @PutMapping("/{id}/profile")
    public Result<Void> updateProfile(@PathVariable Long id, @RequestBody UpdateUserDto userDTO) {
        userDTO.setId(id);
        userService.updateUserProfile(userDTO);
        return Result.success();
    }
} 